package serializer

import (
	"strings"
	"zxs-micro-2/zxs-micro-common/codec/http_codec"
)

type UrlParam struct {
}

func init() {
	http_codec.RegisterHttpSerializer("get/url", &UrlParam{})
}

func (u *UrlParam) Unmarshal(in []byte, body interface{}) error {
	ups := strings.Split(string(in), "&")
	//{"key":"value"}
	mapStr := "{"
	for _, o := range ups {
		kv := strings.Split(o, "=")
		mapStr += "\"" + kv[0] + "\":\"" + strings.Join(kv[1:], "=") + "\","
	}
	mapStr = mapStr[:len(mapStr)-1] + "}"
	return JSONAPI.Unmarshal([]byte(mapStr), body)
}

// Marshal returns the serialized bytes in json protocol.
func (u *UrlParam) Marshal(body interface{}) ([]byte, error) {
	return JSONAPI.Marshal(body)
}
